package com.gmu.controller.excel;

import com.gmu.domain.ResResult;
import com.gmu.pojo.TblStudent;
import com.gmu.pojo.TblTeacher;
import com.gmu.service.TblStudentService;
import com.gmu.service.TblTeacherService;
import com.gmu.service.TblUnitsService;
import com.gmu.service.TblUserService;
import com.gmu.service.excel.ExcelService;
import com.gmu.service.excel.TeacherExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@Api(tags = "导入导出模块")
@RequestMapping("/gmu/teacherexcel")
public class TeacherExcelController {

    @Resource
    private TblUserService userService;
    @Resource
    private TblTeacherService teacherService;
    @Resource
    private TeacherExcelService teacherExcelService;
    @Resource
    private TblUnitsService unitsService;
    @Resource
    private PasswordEncoder passwordEncoder;

    //excel写入数据库
    @ApiOperation("导入数据库")
    @PostMapping("submitFile")
    public ResResult submitFile(MultipartFile file){
        //得到上传的excel文件
        teacherExcelService.saveFile(file,userService,teacherService,unitsService,passwordEncoder);
        return new ResResult(200,"上传成功");
    }

    //将数据库导出excel
    @ApiOperation("导出数据库")
    @GetMapping("out")
    public void out(HttpServletResponse response) throws IOException {
        teacherExcelService.outDataBase(response);
    }

    //根据身份证查询学生信息，在将数据传给前端显示
    @ApiOperation("查询老师信息")
    @PostMapping("ByIdGetTeacher")
    public ResResult ByIdGetTeacher(MultipartFile file){
        Map<String, TblTeacher>  result = teacherExcelService.ByIdGetTeacher(file,teacherService);
        return new ResResult(200,"操作成功",result);
    }

}
